package com.aruba.xfermodeapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 倒影图片
 */
public class InvertImageView extends View {
    private Bitmap bitmapSrc;
    private Bitmap bitmapDst;
    private Paint mPaint = new Paint();


    public InvertImageView(Context context) {
        this(context, null);
    }

    public InvertImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InvertImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        bitmapSrc = BitmapFactory.decodeResource(getResources(), R.mipmap.xyjy6);
        bitmapDst = BitmapFactory.decodeResource(getResources(), R.mipmap.invert_shade);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画原图
        canvas.drawBitmap(bitmapSrc, 0, 0, mPaint);
        //在原图下方画目标图
        canvas.drawBitmap(bitmapDst, 0, bitmapSrc.getHeight(), mPaint);

        Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        mPaint.setXfermode(xfermode);
        Matrix matrix = new Matrix();
        //上下镜像效果对Y轴进行负拉伸
        matrix.setScale(1,-1);
        matrix.postTranslate(0,bitmapSrc.getHeight()*2);
        canvas.drawBitmap(bitmapSrc, matrix, mPaint);
        mPaint.setXfermode(null);
    }
}
